import axios from 'axios';
import Config from 'config';

import Web3Manager from '../../libs/web3-manager';
import AuthenticationManager from '../authentication/authentication-manager';
import GasFee from './gas-fee';

interface FeeWithMinumumAmount {
    gasFee: GasFee;
    minimumAmount: number;
}

export default class FeeManager {
    static getSLP = async (): Promise<FeeWithMinumumAmount> => {
        const headers = {
            [AuthenticationManager.KEY]: AuthenticationManager.getKey(),
            [AuthenticationManager.SECRET]: AuthenticationManager.getSecret(),
            wallet_address: await Web3Manager.getWalletAddress(),
        };

        const call = await axios.get(Config.getAPI() + '/fee/slp', { headers });

        const gasFee = GasFee.fromJSON(call.data);
        gasFee.token = 'SLP';

        return {
            gasFee: gasFee,
            minimumAmount: call.data['minimum_slp'],
        };
    };

    static getEVM = async (
        token: string,
        network: string,
        numberOfTransactions: number,
        disbursementId: number
    ): Promise<GasFee> => {
        const headers = {
            [AuthenticationManager.KEY]: AuthenticationManager.getKey(),
            [AuthenticationManager.SECRET]: AuthenticationManager.getSecret(),
            wallet_address: await Web3Manager.getWalletAddress(),
        };

        const params =
            '?token=' +
            token +
            '&network=' +
            network +
            '&number_of_transactions=' +
            numberOfTransactions +
            '&disbursement_id=' +
            disbursementId;

        const call = await axios.get(Config.getAPI() + '/fee/evm' + params, { headers });

        if (call.data['fee'] && call.data['token']) {
            return GasFee.fromJSON(call.data);
        }

        return call.data;
    };
}
